<?php

namespace App\Dao;

use App\Exceptions\SysExceptions;
use App\Model\GoodsAttributesModel;
use App\Model\ProjectPassModel;
use App\Model\ProjectsModel;
use App\Model\ProjectUserModel;
use App\Services\Logger;
use App\Services\PayServices;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;

class ProjectDao extends BaseDao
{
    public static function get_projects_builder($params = []): Builder
    {
        $query = ProjectsModel::query();
        $query->when(checkIsHasField($params,'project_id'),function ($query)use ($params){
            $query->where('project_id',$params['project_id']);
        });
        $query->when(checkIsHasField($params,'category_id'),function ($query)use ($params){
            $query->where('category_id',$params['category_id']);
        });
        $query->when(checkIsHasField($params,'product_type'),function ($query)use ($params){
            $query->where('product_type',$params['product_type']);
        });

        $query->when(checkIsHasField($params,'search_key'),function ($query)use ($params){
            $query->where('project_name','like','%'.$params['search_key'].'%');
        });

        $query->when(checkIsHasField($params,'show'),function ($query)use ($params){
            $query->where('show',$params['show']);
        });
        return $query;
    }


    /**
     * @throws SysExceptions
     */
    public static function add_project($params = []){

        checkForData(
            ['project_name','category_id','project_image_project_view_url','project_price'],
            ['请填写商品名字','请选择分类','请上传图片','请填写商品价格'],
            $params
        );


        if(self::get_projects_builder()->create($params) === false){
            self::error('创建项目失败');
        }
    }

    public static function get_projects_page(array $all): \Illuminate\Contracts\Pagination\LengthAwarePaginator
    {
        return self::get_projects_builder($all)->orderByDesc('created_at')->paginate();
    }

    public static function edit_project(array $all)
    {

        if(self::get_projects_builder([
            'project_id'=>self::deleteParamId($all,'project_id')
            ])->update(self::filterFields($all,new ProjectsModel())) === false){
            self::error('创建项目失败');
        }
    }

    public static function getOne(array $all,$type = 0)
    {
        $data =  self::get_projects_builder($all)->first();
        if(checkIsHasField($all,'is_add')){
            $data['xiaoliang'] = $data['xiaoliang'] + OrdersDao::get_order_child_builder(['project_id'=>$data['project_id']])
                    ->where('status','<>',0)
                    ->count();
        }

        //查询规格
        $data['sku_colors'] = GoodsAttributesModel::query()->where([
            'goods_id'=>$data['project_id'],
            'type'=>1
        ])->get();
        //尺码
        $data['sku_sizes'] = GoodsAttributesModel::query()->where([
            'goods_id'=>$data['project_id'],
            'type'=>2
        ])->get();

        return $data;
    }

    public static function get_projects($params = [])
    {
        $list =  self::get_projects_builder($params)
            ->where('project_price','>',0)
            ->orderByDesc('created_at')
            ->get();
        return $list;
    }


    /**
     * @throws SysExceptions
     */
    public static function del_project(array $params)
    {
        if(self::get_projects_builder([
                'project_id'=>$params['id']
            ])->delete() === false){
            self::error('删除失败');
        }
    }

    public static function setSkus(array $all)
    {
        if(!checkIsHasField($all,'colors')){
            self::error('请填写新颜色');
        }else if(!checkIsHasField($all,'sizes')){
            self::error('请填写新尺码');
        }
        $colors = $all['colors'];
        $colors = array_unique($colors);
        $has_colors = GoodsAttributesModel::query()->where([
            'goods_id'=>$all['project_id'],
            'type'=>1
        ])->pluck('txt')->toArray();
        if(empty($has_colors)){
            $has_colors = [];
        }
        $insetColors = [];
        foreach ($colors as $values){
            if(!in_array($values,$has_colors) ){
                $insetColors[] = [
                    'goods_id'=>$all['project_id'],
                    'txt'=>$values,
                    'type'=>1
                ];
            }
        }


        $sizes = $all['sizes'];
        $sizes = array_unique($sizes);
        $has_sizes = GoodsAttributesModel::query()->where([
            'goods_id'=>$all['project_id'],
            'type'=>2
        ])->pluck('txt')->toArray();
        if(empty($has_sizes)){
            $has_sizes = [];
        }
        foreach ($sizes as $values){
            if(!in_array($values,$has_sizes) ){
                $insetColors[] = [
                    'goods_id'=>$all['project_id'],
                    'txt'=>$values,
                    'type'=>2
                ];
            }
        }


        if(!empty($insetColors)){
            $result = GoodsAttributesModel::query()->insert($insetColors);
            if($result === false){
                self::error('添加失败');
            }
        }

    }

    public static function getSkus($project_id,$type)
    {
        return GoodsAttributesModel::query()->where('goods_id',$project_id)->where('type',$type)->get();
    }


}
